# 1-导包
from bs4 import BeautifulSoup


# 定义一个封装方法
def parser_html(test_html):
    """
    :param test_html: 待处理的html数据
    :return: 返回三方请求url和三方请求参数
    """

    # 2-实例化BeautifulSoup对象bs
    bs = BeautifulSoup(test_html,"html.parser")
    # 3-通过bs.标签名.get("属性名") --》 提取三方请求url
    url = bs.form.get("action")

    # 4-通过bs对象.find_all("标签名") --》 获取所有的input标签，返回一个所有标签组成的列表
    req_data = {}  # 初始化待返回的三方请求参数字典
    input_list = bs.find_all("input")
    for input in input_list:  # 5-遍历标签列表，提取name和value的值，并组装到新的字典中，作为三方请求的参数
        req_data[input.get("name")] = input.get("value")  # 将获取的三方参数存到字典，相当于键名等于键值

    # 6-返回三方请求url和三方请求参数
    return url, req_data


if __name__ == '__main__':
    test_html = "<form name='easypaysubmit' id='easypaysubmit' target='_blank' method='post' action='http:\/\/121.43.169.97:8000\/muser\/publicRequests'>" \
    "<input name='Version' type='hidden' value='10'\/>" \
    "<input name='CmdId' type='hidden' value='UserRegister'\/>" \
    "<input name='MerCustId' type='hidden' value='6000060007313892'\/>" \
    "<input name='BgRetUrl' type='hidden' value='https:\/\/www.baidu.com\/'\/>" \
    "<input name='RetUrl' type='hidden' value='http:\/\/user-p2p-test.itheima.net\/trust\/chinapnr\/register\/return\/22082322272333301212'\/>" \
    "<input name='UsrId' type='hidden' value=''\/>" \
    "<input name='UsrName' type='hidden' value=''\/>" \
    "<input name='IdType' type='hidden' value='00'\/>" \
    "<input name='IdNo' type='hidden' value='330102199003078870'\/>" \
    "<input name='UsrMp' type='hidden' value='13800000004'\/>" \
    "<input name='UsrEmail' type='hidden' value=''\/>" \
    "<input name='MerPriv' type='hidden' value='22082322272333301212'\/>" \
    "<input name='ChkValue' type='hidden' value='-101'\/>" \
    "<input name='CharSet' type='hidden' value='UTF-8'\/>" \
    "<\/form>" \
    "<script>document.forms['easypaysubmit'].submit();<\/script>"

    res = parser_html(test_html)
    print(res)
